using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Collections;
using System.Text;
using System.Windows.Forms;

using WP.Commons;
using WP.Controls;
using WP.DB;

namespace WP.Modules
{
    public partial class BaseQryRpt : Form
    {
        private MenuInfo m_MenuInfo = new MenuInfo();
        public bool  mblnNeedSaveStartPositionInfo=true ;
        protected bool mblnLookUpSortByDescription = false;
        public ArrayList arraycontrols = new ArrayList();
        protected ArrayList arrayLablecontrols = new ArrayList();
        private bool mblnExitingStatus = false;
        private ToolStrip mtoolStrip = null;

        public bool mblnNeedSetBoolBar = true;
        public bool ExitingStatus
        {
            get
            {
                return mblnExitingStatus;
            }
            set
            {
                mblnExitingStatus = value;
            }
        }

        public bool NeedSetBoolBar
        {
            get
            {
                return mblnNeedSetBoolBar;
            }
            set
            {
                mblnNeedSetBoolBar = value;
                this.mnuToolbarSetting.Enabled = value; 
            }
        }        


        public virtual MenuInfo CurrentMenuInfo
        {
            get
            {
                return m_MenuInfo;
            }
            set
            {
                m_MenuInfo = value;
               if(CvtFunc.ToString(value.strMenuName).Trim()!="")  this.Text = value.strMenuName; 
            }
        }


        public BaseQryRpt()
        {
            InitializeComponent();
            this.CausesValidation = false;
            if (m_MenuInfo.strMenuId == null) m_MenuInfo.strMenuId = "";
            if (m_MenuInfo.strMenuName == null) m_MenuInfo.strMenuName = "";
            if (m_MenuInfo.strProgname == null) m_MenuInfo.strProgname = "";
            if (m_MenuInfo.strTableName == null) m_MenuInfo.strTableName = "";
            if (m_MenuInfo.strNameSpace == null) m_MenuInfo.strNameSpace = "";
            if (m_MenuInfo.strPermit == null) m_MenuInfo.strPermit = "";
            if (m_MenuInfo.strOverViewCondition == null) m_MenuInfo.strOverViewCondition = "";
   
        }

        public void BaseQryRpt_Load(object sender, EventArgs e)
        {
            if (Application.ProductName.IndexOf("Microsoft") >= 0 && !SysVar.IsByOutlookCallBy) return;
            if (this.MaximizeBox == true && (this.FormBorderStyle == FormBorderStyle.Sizable || this.FormBorderStyle == FormBorderStyle.SizableToolWindow))
            {
                if(!this.Modal) this.WindowState = FormWindowState.Maximized;

            }
            else
            {
                this.WindowState = FormWindowState.Normal;

                if ((SysVar.IsByOutlookCallBy ||   mblnNeedSaveStartPositionInfo) && this.Location.X >=0)
                {
                    
                    DBAccess dbcon = new DBAccess(false, false);
                    DataRow dr = dbcon.GetDataFirstRow("select top 1 * from muserFormStartStatus where Parameter=" + this.CurrentMenuInfo.iParameter.ToString() + " and  FormName='" + this.Name + "' and username='" + SysVar.UserName + "' and IsCallbyOutLook=" + (SysVar.IsByOutlookCallBy ? "1" :"0")  );
                    if (dr != null)
                    {
                        this.StartPosition = FormStartPosition.Manual;
                        Point oPoint = new Point(CvtFunc.ToInt(dr["StartPostionX"]), CvtFunc.ToInt(dr["StartPostionY"]));
                        if (oPoint.X >= 0 && oPoint.Y >= 0)
                        {
                            this.Location = new Point(CvtFunc.ToInt(dr["StartPostionX"]), CvtFunc.ToInt(dr["StartPostionY"]));
                        }
                        if (this.FormBorderStyle == FormBorderStyle.Sizable || this.FormBorderStyle == FormBorderStyle.SizableToolWindow)
                        {
                            this.Size = new Size(CvtFunc.ToInt(dr["FormWidth"]), CvtFunc.ToInt(dr["FormHeight"]));
                        }
                    }
                    dbcon.CloseCon();
                }
                if(SysVar.MDIParentForm!=null )  SysVar.MDIParentForm.Refresh();
            }
            SetBackColor();
            LoadControl();
            this.helpProvider.SetHelpNavigator(this, HelpNavigator.Topic);   
            if (CvtFunc.ToString( this.CurrentMenuInfo.strMenuId) != "")
                this.helpProvider.SetHelpKeyword(this, DataFunctions.GetFieldValue("mhelptopicdict", "HelpTopic", "MenuId='" + this.CurrentMenuInfo.strMenuId + "'"));
            else
               this.helpProvider.SetHelpKeyword(this, DataFunctions.GetFieldValue("mhelptopicdict", "HelpTopic", "progname='" + this.Name +"'"));  
            Graphics g = this.CreateGraphics();
            DataFunctions.SetDftLanguage(g,this.arraycontrols, this.Name);   
        }


        public bool NeedValiding()
        {
            System.Reflection.PropertyInfo pInfo = this.GetType().GetProperty("formStatus");
            FormStatus formStatus = FormStatus.Update;
            if (pInfo != null) formStatus =(FormStatus)pInfo.GetValue(this, null);
            BaseMtn objBaseMtn =this.Parent!=null && this.Parent.FindForm() != null && this.Parent.FindForm() as BaseMtn != null ? this.Parent.FindForm() as BaseMtn  : null;

            if (formStatus != FormStatus.Update  || (objBaseMtn != null && (objBaseMtn.formStatus != FormStatus.Update || objBaseMtn.ExitingStatus )))
                return false ;
            else 
                return true;
        }

        public void InitLanguageSetting()
        {
            arrayLablecontrols.Sort(new mySortControlByName());
            DataFunctions.InitiateLanguageInfo(this.arraycontrols, arrayLablecontrols, this.Name);
        }
        private void LoadControl()
        {
            foreach (object con in this.arraycontrols)
            {
                if ((con as Control) != null && (con as Control).GetType().Name == "ButtonEx")
                {
                    (con as Control).TabStop = false;
                }
            }
        }
        protected virtual void SetBackColor()
        {
            this.BackColor = SysVar.CurrrentColorTable.MenuStripGradientBegin;   
        }

        private void BaseQryRpt_KeyPress(object sender, KeyPressEventArgs e)
        {
            TextBox txt = this.ActiveControl as TextBox;
            if (e.KeyChar.ToString() == "\r")
            {
                if (!(txt != null && txt.Multiline))
                {
                    e.Handled = true;

                    TabPage tabPage = GetParentTabPage(this.ActiveControl);

                    if (tabPage != null && this.ActiveControl.Name == GetLastAbleFocusControlInContainer(tabPage).Name)
                    {
                        TabControl tabcon = tabPage.Parent as TabControl;
                        if (tabcon != null && tabcon.SelectedIndex < tabcon.TabCount - 1)
                        {
                            tabcon.SelectedIndex = tabcon.SelectedIndex + 1;
                            Control con = GetFirstAbleFocusControlInContainer(tabcon.TabPages[tabcon.SelectedIndex]);
                            con.Focus();
                        }
                        else
                        {
                            SelectNextControl(this.ActiveControl, true, true, true, true);
                        }
                    }
                    else if (this.ActiveControl!=null && this.ActiveControl.GetType().Name == "SplitContainer")
                    {

                        SendKeys.Send("{Tab}");
                    }
                    else
                    {
                        SelectNextControl(this.ActiveControl, true, true, true, true);
                    }

                }

            }

        }

        private Control GetLookUpControl(TextBox txtControl)
        {
            ArrayList conLookup = new ArrayList(); 
            Control conParent = txtControl.Parent as Control;
            if (conParent == null) return null;
            foreach (Control con in conParent.Controls)
            {
                //if (con.GetType().Name == "ButtonEx")
                //{
                //    MsgBoxEx.Show("sdfs");  
                //}
                if (con.GetType().Name == "ButtonEx" && con.Left + con .Width >= txtControl.Left + txtControl.Width && con.Top >= txtControl.Top && con.Top <= txtControl.Top + txtControl.Height)
                {
                    conLookup.Add(con); 
                }
            }
            conLookup.TrimToSize(); 
            if (conLookup.Capacity == 0) return null;
            if (conLookup.Capacity == 1) return conLookup[0] as Control ;
            Control conLatest = conLookup[0] as Control ;
            foreach (Control con in conLookup)
            {
                if (con.Left < conLatest.Left) conLatest = con; 
            }
            return conLatest;
        }

        private void BaseQryRpt_KeyDown(object sender, KeyEventArgs e)
        {
            Control con = this.ActiveControl;
            if (con != null && (e.KeyCode == SysVar.SystemKey.LookUPKey || e.KeyCode == SysVar.SystemKey.LookUPSecKey))
            {
                mblnLookUpSortByDescription = (e.KeyCode == SysVar.SystemKey.LookUPSecKey); 
                if (con.GetType().Name == "TextBox")
                {
                    Control conlookup = GetLookUpControl(con as TextBox );
                    if(conlookup!=null)  ubtLookUp_Click(conlookup, new EventArgs());
                }
                else if (con.GetType().Name == "ButtonEx")
                {
                    ubtLookUp_Click(con, new EventArgs());
                }

            }
            if (((Control.ModifierKeys & Keys.Shift) == Keys.Shift && e.KeyCode == Keys.Down && e.KeyCode == SysVar.SystemKey.SearchByNameKey) || (e.KeyCode != Keys.Down && e.KeyCode == SysVar.SystemKey.SearchByNameKey))
            {
                GoogleSearch(true);
            }
            else if (e.KeyCode == SysVar.SystemKey.SearchByCodeKey)
            {
                GoogleSearch(false);
            }

        }
        protected virtual void GoogleSearch(bool blnIsByDescription)
        {
            return;
        }
        private void BaseQryRpt_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (this.WindowState != FormWindowState.Maximized)
            {
                DBAccess dbcon = new DBAccess(false, false);
                if (mblnNeedSaveStartPositionInfo)
                {

                    string strSql = "Delete from muserFormStartStatus where  Parameter=" + this.CurrentMenuInfo.iParameter.ToString() + " and  FormName='" + this.Name + "' and username='" + SysVar.UserName + "' and IsCallbyOutLook=" + (SysVar.IsByOutlookCallBy ? "1" :"0") +
                                           "; Insert into muserFormStartStatus (Parameter,FormName,username,StartPostionX,StartPostionY,FormWidth,FormHeight,IsCallbyOutLook) values (" +
                                           this.CurrentMenuInfo.iParameter.ToString() + ",'" +
                                           this.Name + "','" + SysVar.UserName + "'," + this.Location.X.ToString() + "," + this.Location.Y.ToString() + "," + this.Size.Width.ToString() +
                                           "," + this.Size.Height.ToString() + ","+(SysVar.IsByOutlookCallBy ? "1" :"0") + ")";
                    dbcon.ExecuteSql(strSql);

                }
                dbcon.CloseCon();
            }



        }
        protected virtual void ubtLookUp_Click(object sender, EventArgs e)
        {
            return;
        }

        private Control GetLastAbleFocusControlInContainer(Control ContainerControl)
        {
            if (IsContainerControl(ContainerControl))
            {
                if (ContainerControl.Controls.Count == 0) return ContainerControl;
                ArrayList ctls = new ArrayList(ContainerControl.Controls);
                ctls.Sort(new mySortControlByIndex());

                for (int i = ctls.Count - 1; i >= 0; i--)
                {
                    Control con = ctls[i] as Control;
                    if (ExistsCanFocusNonContainerControl(con))
                    {
                        if (IsContainerControl(con))
                            return GetLastAbleFocusControlInContainer(con);
                        else if (con.TabStop)
                            return con;
                    }
                    else
                        continue;
                }

            }
            return ContainerControl;
        }
        private Control GetFirstAbleFocusControlInContainer(Control ContainerControl)
        {

            if (IsContainerControl(ContainerControl))
            {
                if (ContainerControl.Controls.Count == 0) return ContainerControl;
                ArrayList ctls = new ArrayList(ContainerControl.Controls);
                ctls.Sort(new mySortControlByIndex());

                foreach (Control con in ctls)
                {
                    if (ExistsCanFocusNonContainerControl(con))
                    {
                        if (IsContainerControl(con))
                            return GetFirstAbleFocusControlInContainer(con);
                        else
                            return con;
                    }
                    else
                        continue;
                }
            }
            return ContainerControl;
        }
        private TabPage GetParentTabPage(Control con)
        {
            if (con == null) return null; 
            Control conParent = con.Parent;
            for (int i = 0; i < 7; i++)
            {
                if (conParent != null && conParent.GetType().Name == "TabPage")
                    return conParent as TabPage;
                else if (conParent != null)
                    conParent = conParent.Parent;
            }
            return null;
        }
        public class mySortControlByIndex : IComparer
        {
            int IComparer.Compare(Object x, Object y)
            {
                Control xcon = x as Control;
                Control ycon = y as Control;
                return xcon.TabIndex - ycon.TabIndex;
            }
        }
        public class mySortControlByName : IComparer
        {
            int IComparer.Compare(Object x, Object y)
            {
                Control xcon = x as Control;
                Control ycon = y as Control;
                return string.Compare(xcon.Name, ycon.Name);
            }
        }
        public class myFindControlByName : IComparer
        {
            int IComparer.Compare(Object x, Object y)
            {
                Control con = x as Control;
                if (con != null)
                {
                    return string.Compare(con.Name, y.ToString(), true);
                }
                else
                {
                    con = y as Control;
                    return string.Compare(con.Name, x.ToString(), true);
                }
            }
        }
        private bool IsContainerControl(Control con)
        {

            if (BaseFunctions.InStrEx("TabControl,TabPage,GroupBox,FlowLayoutPanel,TableLayoutPanel,SplitContainer,SplitterPanel", con.GetType().Name))
                return true;
            else
                return false;
        }
        private bool ExistsCanFocusNonContainerControl(Control con)
        {
            ArrayList arrcon = new ArrayList(con.Controls);
            if (arrcon.Count == 0 || con.GetType().Name == "TextRadioBox" || con.GetType().Name == "ComboBoxEx" || con.GetType().Name == "DataGridEx")
            {
                return con.CanFocus && !IsContainerControl(con) && (con.TabStop == true);
            }
            else
            {
                foreach (Control oCon in arrcon)
                {
                    bool bReturn = ExistsCanFocusNonContainerControl(oCon);
                    if (bReturn) return true;
                }
            }
            return false;
        }
        protected void LoadAllControl(ArrayList ctls)
        {
            foreach (Control con in ctls)
            {

                if (con.GetType().Name == "ToolStrip" && (con as ToolStrip).ContextMenuStrip == null)
                {
                    (con as ToolStrip).ContextMenuStrip = this.mnuToolbarSetting;
                    mtoolStrip = (con as ToolStrip);
                    SetAlwaysImageAdnTextBoolBar();
                }
                if (con.Controls.Count > 0 && con.GetType().Name != "TextRadioBox" && con.GetType().Name != "DateTimeEx")
                {
                    arraycontrols.Add(con);
                    if (con.GetType().Name == "Label")
                    {
                        Label oLabel = new Label();
                        oLabel.Name = con.Name;
                        oLabel.Text = con.Text;
                        arrayLablecontrols.Add(oLabel);
                    }
                    ArrayList concx = new ArrayList(con.Controls);
                    LoadAllControl(concx);
                }
                else
                {
                    arraycontrols.Add(con);
                    if (con.GetType().Name == "Label")
                    {
                        Label oLabel = new Label();
                        oLabel.Name = con.Name;
                        oLabel.Text = con.Text;
                        arrayLablecontrols.Add(oLabel);
                    }
                }
            }
        }



        public Control GetControl(string strControlName)
        {
            this.arraycontrols.Sort(new mySortControlByName());   
            int iIndex = this.arraycontrols.BinarySearch(strControlName, new myFindControlByName());
            if (iIndex >= 0)
                return arraycontrols[iIndex] as Control;
            else
                return null;
        }

        public virtual void BaseQryRpt_BackColorChanged(object sender, EventArgs e)
        {
            foreach (Object  con in this.arraycontrols)
            {
                if ((con as Control) != null && (con as Control).GetType().Name == "TabPage")
                {
                    (con as Control).BackColor = this.BackColor;
                }
                else if (con.GetType().Name == "DataGridView")
                {
                    (con as DataGridView).BackgroundColor = SysVar.CurrrentColorTable.ToolStripGradientEnd;

                }
                else if (con.GetType().Name == "DataGridEx")
                {
                    (con as DataGridEx).BackgroundColor = SysVar.CurrrentColorTable.ToolStripGradientEnd;
                }
                else if ( con.GetType().Name == "DataGridCom")
                {
                    (con as DataGridCom).BackgroundColor = SysVar.CurrrentColorTable.ToolStripGradientEnd;
                }
                else if ((con as Form) != null)
                {
                    (con as Form).BackColor = this.BackColor;
                }
            }
        }
        public void SetGridMaxLength(DataGridView dgv, string strTableName)
        {
            DBAccess dbcon = new DBAccess(false, false);
            string strSql = @"SELECT syscolumns.name AS FieldName, systypes.name AS Type, syscolumns.length,replace(replace(replace(syscomments.text,'(',''),')',''),'''','') as DefValue
					FROM syscolumns INNER JOIN systypes ON syscolumns.xtype = systypes.xtype INNER JOIN
					sysobjects ON syscolumns.id = sysobjects.id INNER JOIN syscomments on syscomments.id=syscolumns.cdefault
					WHERE (systypes.status <> 1) AND (sysobjects.name = '{0}')";
            strSql = string.Format(strSql, strTableName);
            DataView dvMaxLenAndDef = new DataView(dbcon.GetDataSet(strSql).Tables[0]);

            dbcon.CloseCon();
            for (int i = 0; i < dgv.Columns.Count; i++)
            {
                dvMaxLenAndDef.RowFilter = "FieldName='" + dgv.Columns[i].Name + "'";
                if (dvMaxLenAndDef.Count > 0)
                {
                    string strType = dvMaxLenAndDef[0]["Type"].ToString().ToLower();
                    int intMaxLength = int.Parse(dvMaxLenAndDef[0]["Length"].ToString());
                    if (strType == "char" || strType == "varchar")
                    {
                        DataGridViewTextBoxColumn col = dgv.Columns[i] as DataGridViewTextBoxColumn;
                        if (col != null)
                        {
                            col.MaxInputLength = intMaxLength;
                        }
                    }
                }
            }
        }

        public virtual void OnHelp()
        {
            if (this.helpProvider.GetHelpKeyword(this).Trim() != "")
                Help.ShowHelp(this, this.helpProvider.HelpNamespace, this.helpProvider.GetHelpNavigator(this), this.helpProvider.GetHelpKeyword(this));
            else
                Help.ShowHelp(this, this.helpProvider.HelpNamespace);



        }
        public void SetGridViewDelImage(DataGridView gdvGrid, int iCurrentRow, int iDelCol)
        {
            bool blnFirstEnterGrid = true;
            foreach (DataGridViewRow gRow in gdvGrid.Rows)
            {
                if (CvtFunc.ToInt(gRow.Cells[iDelCol].Tag) == 1)
                {
                    blnFirstEnterGrid = false;
                    break;
                }
            }
            foreach (DataGridViewRow gRow in gdvGrid.Rows)
            {
                if (gRow.Index == iCurrentRow)
                {
                    gdvGrid.Tag = CvtFunc.ToInt(gRow.Cells[iDelCol].Tag) == 1 || blnFirstEnterGrid;
                    gRow.Cells[iDelCol].Value = this.iglSource.Images["del.ico"];
                    gRow.Cells[iDelCol].Tag = 1;
                }
                else
                {
                    gRow.Cells[iDelCol].Value = this.iglSource.Images["blank.ico"];
                    gRow.Cells[iDelCol].Tag = 0;
                }
            }
        }

        public void SetGridViewDelImage(DataGridView gdvGrid, DataGridViewCellEventArgs e, int iDelCol)
        {
            foreach (DataGridViewRow gRow in gdvGrid.Rows)
            {
                if (gRow.Index == e.RowIndex)
                {
                    gRow.Cells[iDelCol].Value = this.iglSource.Images["del.ico"];
                    gRow.Cells[iDelCol].Tag = (e.ColumnIndex != iDelCol ? 1 : 0);
                }
                else
                {
                    gRow.Cells[iDelCol].Value = this.iglSource.Images["blank.ico"];
                    gRow.Cells[iDelCol].Tag = 0;
                }
            }
        }
        public bool HasDelImage(DataGridView gdvGrid, DataGridViewCellEventArgs e)
        {


            if (e.RowIndex>=0 && CvtFunc.ToInt(gdvGrid[e.ColumnIndex, e.RowIndex].Tag) <= 0)
            {
                if (gdvGrid[e.ColumnIndex, e.RowIndex].Selected)
                {
                    gdvGrid[e.ColumnIndex, e.RowIndex].Tag = 1;
                    return true;
                }
                else
                {
                    gdvGrid[e.ColumnIndex, e.RowIndex].Tag = 1;

                    return false;
                }
            }
            return true;
        }
        public void SetBlankImage(DataGridView grid)
        {

            for (int i = 0; i < grid.Columns.Count; i++)
            {
                if (grid.Columns[i].GetType().Name == "DataGridViewImageColumn")
                {
                    foreach (DataGridViewRow gRow in grid.Rows)
                    {
                        if (gRow.Cells[i].Value == null)
                        {
                            gRow.Cells[i].Value = this.iglSource.Images["blank.ico"];
                        }
                    }
                }
            }

        }


        private void showTextTipToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.showTextTipToolStripMenuItem.Checked = !this.showTextTipToolStripMenuItem.Checked;
            if (mtoolStrip == null) return;
            foreach (ToolStripItem oItem in mtoolStrip.Items)
            {
                if (oItem.GetType().Name != "ToolStripButton" && oItem.GetType().Name != "ToolStripSplitButton") continue;
                if (oItem.GetType().Name == "ToolStripButton")
                {
                    (oItem as ToolStripButton).TextImageRelation = TextImageRelation.ImageBeforeText;
                    (oItem as ToolStripButton).DisplayStyle = this.showTextTipToolStripMenuItem.Checked ? ToolStripItemDisplayStyle.ImageAndText  : ToolStripItemDisplayStyle.Image;
                }
                else
                {
                    (oItem as ToolStripSplitButton).TextImageRelation = TextImageRelation.ImageBeforeText;
                    (oItem as ToolStripSplitButton).DisplayStyle = this.showTextTipToolStripMenuItem.Checked ? ToolStripItemDisplayStyle.ImageAndText : ToolStripItemDisplayStyle.Image;
                }
            }
        }

        private void SetAlwaysImageAdnTextBoolBar()
        {
            if (mtoolStrip == null) return;
            foreach (ToolStripItem oItem in mtoolStrip.Items)
            {
                if (oItem.GetType().Name != "ToolStripButton" && oItem.GetType().Name != "ToolStripSplitButton" && oItem.GetType().Name != "ToolStripDropDownButton") continue;

                if (oItem.GetType().Name == "ToolStripButton")
                {
                    if ((oItem as ToolStripButton).DisplayStyle != ToolStripItemDisplayStyle.Text)
                    {
                        (oItem as ToolStripButton).TextImageRelation = TextImageRelation.ImageBeforeText;
                        (oItem as ToolStripButton).DisplayStyle = ToolStripItemDisplayStyle.ImageAndText;
                    }
                }
                else if (oItem.GetType().Name == "ToolStripDropDownButton")
                {
                    (oItem as ToolStripDropDownButton).TextImageRelation = TextImageRelation.ImageBeforeText;
                    (oItem as ToolStripDropDownButton).DisplayStyle = ToolStripItemDisplayStyle.ImageAndText;

                }
                else
                {
                    (oItem as ToolStripSplitButton).TextImageRelation = TextImageRelation.ImageBeforeText;
                    (oItem as ToolStripSplitButton).DisplayStyle = ToolStripItemDisplayStyle.ImageAndText;
                }
            }
        }

        public void AfterClickPrtBtn(PrintInfo printInfo)
        {
            BaseFunctions.AutoExitRptForm(this, printInfo.PrintType == PrintInfo.OutputType.Preview);
        }

        private DataView mdvOverViewList = null;
        private DataGridView mGridViewList = null;

        public void ExportToExcel(DataGridEx GridLst)
        {
            try
            {
                DBAccess dbcon = new DBAccess(false, false);
                string strSQL = CvtFunc.ToString(GridLst.CurrentSql);
                if (strSQL == "") strSQL = GridLst.Sql;
                DataView dv = dbcon.GetDataTable(strSQL).DefaultView;
                mdvOverViewList = dv;
                mGridViewList = GridLst as DataGridView;
                if (!bkWorkerExportToExcel.IsBusy) bkWorkerExportToExcel.RunWorkerAsync();
                dbcon.CloseCon();
            }
            catch (Exception e)
            {
                MsgBoxEx.Show(e.Message, SysVar.WarnTitle);
            }
        }

        public void ExportToExcel(DBAccess dbcon, DataGridEx GridLst)
        {
            try
            {
                string strSQL = CvtFunc.ToString(GridLst.CurrentSql);
                if (strSQL == "") strSQL = GridLst.Sql;
                DataView dv = dbcon.GetDataTable(strSQL).DefaultView;
                mdvOverViewList = dv;
                mGridViewList = GridLst as DataGridView;
                if (!bkWorkerExportToExcel.IsBusy) bkWorkerExportToExcel.RunWorkerAsync();
            }
            catch (Exception e)
            {
                MsgBoxEx.Show(e.Message, SysVar.WarnTitle);
            }
        }

        private void bkWorkerExportToExcel_DoWork(object sender, DoWorkEventArgs e)
        {
            ToExcel.DataGirdViewToExcel(mdvOverViewList, mGridViewList);
        }


    }
}